/**
 * 
 */
package reports;

import java.io.File;
import java.io.InputStream;

import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;

import org.springframework.core.io.ClassPathResource;

import util.FuncionesUtiles;

/**
 * @author Fede
 * 
 */
public class GestionReporte {

	private static final String REPORTS_LOCATION = "reports/";

	public static void generarReporte(ReportData reportData) throws Exception {
		
		String reportRelativePath = new String(REPORTS_LOCATION + reportData.getReportName());
		String absolutePathPdf = System.getProperty("user.dir") + File.separator + reportData.getReportName() + ".pdf";
//		String absolutePathPdf = "/home/insur/Escritorio/Lista de Precios" + File.separator + reportData.getReportName() + ".pdf";
		
		ClassPathResource cpr = new ClassPathResource(reportRelativePath + ".jasper");
		InputStream in = cpr.getInputStream();
		JasperReport jasperReport = (JasperReport) JRLoader.loadObject(in);		
		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
				reportData.getParameters(), reportData.getDataSource());
		
        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, absolutePathPdf);
        exporter.exportReport();
        
        FuncionesUtiles.openPdf(absolutePathPdf);		
//		JasperViewer.viewReport(jasperPrint, false);
	}
	
}
